const express = require('express')
const fs = require('fs')
const path = require('path')

const app = express()

let recordMiddleware = function(req, res, next) {
  fs.appendFile(path.resolve(__dirname, './log.txt'), `\r\n${req.path} | ${req.ip}`, error => {
    if(error) {
      console.log('写入日志错误')
    }
  })
  next()
}

app.use(recordMiddleware)

app.get('/', (req, res) => {
  res.send('首页')
})

app.get('/user', (req, res) => {
  res.send('深田咏美')
})

app.all('*url', (req, res) => {
  res.send('404 找不到资源')
})

app.listen(9000, () => {
  console.log('服务已启动，9000端口监听中。。。')
})